package com.cloudree.gllib.program;

import android.content.Context;
import android.opengl.GLES20;
import android.support.annotation.RawRes;

import com.cloudree.gllib.util.GLHelper;

/**
 * Created by Administrator on 2017/10/17 0017.
 */
public abstract class ShaderProgram {
    // Uniform constants
    protected static final String U_MVP_MATRIX = "uMVPMatrix";
    protected static final String U_MATRIX = "uMatrix";
    protected static final String U_COLOR = "uColor";
    protected static final String U_TEXTURE_UNIT = "uTextureUnit";

    // Attribute constants
    protected static final String A_POSITION = "aPosition";
    protected static final String A_COLOR = "aColor";
    protected static final String A_TEXTURE = "aTexture";

    // Shader program
    protected int program;

    public ShaderProgram(Context context, @RawRes int vertexId, @RawRes int fragmentId, String... attr) {
        int vertexShader = GLHelper.generateShader(GLES20.GL_VERTEX_SHADER, GLHelper.readRawFile(context, vertexId));
        int fragmentShader = GLHelper.generateShader(GLES20.GL_FRAGMENT_SHADER, GLHelper.readRawFile(context, fragmentId));

        this.program = GLHelper.generateGlProgram(vertexShader, fragmentShader, attr);
    }


    /**
     * use program
     */
    public void useProgram() {
        GLES20.glUseProgram(program);
    }
}
